/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.wifibrowser.lib;

import java.lang.reflect.Field;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年11月12日 上午11:38:49
 * @Title <B>todo</B>
 * 
 *        <pre>
 * todo
 * </pre>
 */

public abstract class BaseFragment extends Fragment {
	protected Activity act;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.act = activity;
	}
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 * <pre>todo</pre>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		int layoutResId=onCreateView();
		return inflater.inflate(layoutResId, container, false);
	}
	
	@Override
	public final void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		init(view,savedInstanceState);
	}
	
	protected abstract @LayoutRes int onCreateView();
	protected abstract void init(View container,Bundle savedInstanceState);

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
		try {
			Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
			childFragmentManager.setAccessible(true);
			childFragmentManager.set(this, null);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public void showProgDialog(String msg) {
		if (act != null && act instanceof BaseActivity) {
			((BaseActivity) act).showProgDialog();
		}
	}

	public void disProgDialog() {
		if (act != null && act instanceof BaseActivity) {
			((BaseActivity) act).disProgDialog();
		}
	}

	public void toast(String text){
		if (act != null && act instanceof BaseActivity) {
			((BaseActivity) act).toast(text);
		}
	}

}
